package club.xyes.zkh.ecommerce.commons.util;

import club.xyes.zkh.ecommerce.commons.entity.Coupon;
import club.xyes.zkh.ecommerce.commons.entity.Shop;
import club.xyes.zkh.ecommerce.commons.entity.User;
import club.xyes.zkh.ecommerce.commons.entity.UserMembership;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

/**
 * Create by 郭文梁 2019/4/20 0020 14:54
 * DiscountUtil
 * 优惠信息计算相关工具类
 *
 * @author 郭文梁
 * @data 2019/4/20 0020
 */
@Slf4j
public class DiscountUtil {
    public static int getMembershipDiscount(UserMembership membership, User user, Shop shop, int fee) {
        return 0;
    }

    /**
     * 检查优惠券是否在可用时间段内
     *
     * @param coupon 优惠券对象
     * @param now    当前时间
     * @return 是否可用
     */
    public static boolean isCouponInTime(Coupon coupon, Date now) {
        long nowTime = now.getTime();
        if (nowTime > coupon.getEndTime().getTime() || nowTime < coupon.getStartTime().getTime()) {
            log.debug("Check coupon time unavailable; current:[{}], start:[{}], end:[{}]", now, coupon.getStartTime(), coupon.getEndTime());
            return false;
        }
        int weekStart = coupon.getWeekRangeStart();
        int weekEnd = coupon.getWeekRangeEnd();
        int dayOfWeek = DateTimeUtil.getDayOfWeek(now);
        if (dayOfWeek < weekStart || dayOfWeek > weekEnd) {
            log.debug("Check coupon week unavailable; current:[{}], start:[{}], end:[{}]", dayOfWeek, weekStart, weekEnd);
            return false;
        }
        long nowNoDateTime = DateTimeUtil.cleanDate(now).getTime();
        long startNoDateTime = coupon.getTimeRangeStart() == null ? 0 : DateTimeUtil.cleanDate(coupon.getTimeRangeStart()).getTime();
        long endNoDateTime = coupon.getTimeRangeEnd() == null ? Long.MAX_VALUE : DateTimeUtil.cleanDate(coupon.getTimeRangeEnd()).getTime();
        boolean inTime = nowNoDateTime >= startNoDateTime && nowNoDateTime <= endNoDateTime;
        if (!inTime) {
            log.debug("Check coupon time range unavailable; current:[{}], start:[{}], end:[{}]", now, coupon.getStartTime(), coupon.getEndTime());
        }
        return inTime;
    }
}
